package com.cyan.learn;

import java.util.HashSet;

/**
 * 链表算法
 */
public class LinkedListTest {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode();
        ListNode pre = dummy;

        int f = 0; // 表示满十进一
        while (l1 != null && l2 != null) {
            ListNode node = new ListNode();
            int sum = l1.val + l2.val + f;
            if (sum >= 10) {
                node.val = sum - 10;
                f = 1;
            } else {
                node.val = sum;
                f = 0;
            }
            l1 = l1.next;
            l2 = l2.next;
            pre.next = node;
            pre = pre.next;
        }

        while (l1 != null) {
            int v = l1.val + f;
            if (v >= 10) {
                pre.next = new ListNode(v - 10);
                f = 1;
            } else {
                pre.next = new ListNode(v);
                f = 0;
            }
            l1 = l1.next;
            pre = pre.next;
        }

        while (l2 != null) {
            int v = l2.val + f;
            if (v >= 10) {
                pre.next = new ListNode(v - 10);
                f = 1;
            } else {
                pre.next = new ListNode(v);
                f = 0;
            }
            l2 = l2.next;
            pre = pre.next;
        }

        if (f != 0) {
            pre.next = new ListNode(f);
        }

        return dummy.next;
    }

    /**
     * 203丶移除链表元素
     * 使用虚拟节点
     */
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode(); // 虚拟头节点
        dummy.next = head;

        ListNode cur = dummy; // 当前元素。删除的元素为 cur.next 需要有前驱元素，要不删不了
        while (cur.next != null) {
            if (cur.next.val == val) {
                // 删除 cur.next 元素
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }

        return dummy.next;
    }

    /**
     * 203丶移除链表元素
     * 不使用虚拟头节点
     */
    public ListNode removeElements2(ListNode head, int val) {
        while (head != null && head.val == val) {
            head = head.next;
        }

        if (head == null) {
            return head;
        }

        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == val) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }

        return head;
    }


    /**
     * 206丶反转链表
     * 双指针
     */
    public ListNode reverseList(ListNode head) {
        ListNode cur = head;
        ListNode pre = null;

        while (cur != null) {
            ListNode temp = cur.next; // 临时指针，保存下一个指针的地址
            cur.next = pre;
            pre = cur;
            cur = temp;
        }

        return pre;
    }


    /**
     * 24丶两两交换链表中的节点
     * 使用虚拟头节点
     */
    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode();
        dummy.next = head;

        ListNode cur = dummy;
        while (cur.next != null && cur.next.next != null) {
            ListNode t1 = cur.next;
            ListNode t2 = cur.next.next;
            ListNode t3 = cur.next.next.next;

            cur.next = t2;
            t2.next = t1;
            t1.next = t3;

            cur = t1; // 因为t1已经交换到第二个位置了
        }

        return dummy.next;
    }

    /**
     * 19丶删除链表中倒数第N个节点
     * 双指针：快慢指针
     */
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode();
        dummy.next = head;

        ListNode slow = dummy, fast = dummy;
        for (int i = 0; i < n + 1; i++) {
            fast = fast.next;
        }

        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }

        slow.next = slow.next.next;
        return dummy.next;
    }

    /**
     * 142丶环形链表Ⅱ
     * 双指针：快慢指针
     * 如果有环，那么两个指针在不同速度下一定会相遇
     */
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;

            if (fast == slow) { // 快慢指针相遇了
                ListNode a = fast;
                ListNode b = head;
                while (a != b) {
                    a = a.next;
                    b = b.next;
                }
                return a;
            }
        }

        return null;
    }

    /**
     * 160丶相交链表
     */
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        HashSet<ListNode> set = new HashSet<>();
        while (headA != null) {
            set.add(headA);
            headA = headA.next;
        }

        while (headB != null) {
            if (set.contains(headB)) {
                return headB;
            } else {
                headB = headB.next;
            }
        }

        return null;
    }

    /**
     * 链表节点
     */
    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
